﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace SGUI
{
    public class SGLabel : UIElement
    {
        
        
        public override void Init()
        {
            base.Init();
            this.AddProperty("Font", UIManager.DefaultFont);
            this.AddProperty("Text", "Label", Property_Text_Set);
            this.AddProperty("FontSize",24);
        }
        public override void OnEnter()
        {
            GenRender();
            this.GenSelfMesh();
            UpdateToRender();
            base.OnEnter();
        }
        public void Property_Text_Set(object newValue, object oldValue)
        {
            HasChanged = true;
            if (this.GetProperty("Font").Value != null)
            {
               
            }
        }

        public override void GenSelfMesh()
        {
            this.FillMesh();
           
        }
         
        void FillMesh() 
        {
            string strText = (string)this.GetProperty("Text").Value;
            SGUIFont font = (SGUIFont)this.GetProperty("Font").Value;
            int fontSize = (int)this.GetProperty("FontSize").Value;
            font.RequestText(strText, fontSize);
            this.mGeom = new UIGeometry();
            this.mGeom.verts = new List<Vector3>();
            this.mGeom.uvs = new List<Vector2>();
            this.mGeom.cols = new List<Color>();
            Vector2 sizev = font.CalculatePrintedSize(strText,fontSize);
            this.SetProperty("Width",(double)sizev.x,false);
            this.SetProperty("Height", (double)sizev.y, false);
            font.Print(strText,fontSize,SGTools.ColorFromHTML(this.PropertyValue<string>("Color")),this.mGeom.verts,this.mGeom.uvs,this.mGeom.cols,(float)this.PropertyValue<double>("Width"),this.GetPivotOffset());
        }

        void GenRender()
        {
            SGUIFont font = (SGUIFont)this.GetProperty("Font").Value;
            this.gameObject.AddComponent<MeshRenderer>();
            this.gameObject.AddComponent<MeshFilter>();
        }

        public override void UpdateToRender()
        {
            SGUIFont font = (SGUIFont)this.GetProperty("Font").Value;
            string strText = (string)this.GetProperty("Text").Value;

            Mesh mesh = new Mesh();
            mesh.vertices = this.mGeom.verts.ToArray();
            mesh.colors = this.mGeom.cols.ToArray();
            mesh.uv = this.mGeom.uvs.ToArray();

            int[] indices = new int[strText.Length * 6];
            for (int i = 0; i < strText.Length; i++)
            {
                indices[i * 6 + 0] = i * 4 + 2;
                indices[i * 6 + 1] = i * 4 + 1;
                indices[i * 6 + 2] = i * 4 + 0;

                indices[i * 6 + 3] = i * 4 + 2;
                indices[i * 6 + 4] = i * 4 + 0;
                indices[i * 6 + 5] = i * 4 + 3;

            }
            mesh.SetIndices(indices, MeshTopology.Triangles, 0);
            MeshRenderer render = this.gameObject.GetComponent<MeshRenderer>();
            MeshFilter mf = this.gameObject.GetComponent<MeshFilter>();
            mf.sharedMesh = mesh;
            render.material = font.font.material;
        }
         
    }
}
